/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var Item = new Class({
    
    Implements: [Options,Events],
    
    revision: 0,
    
    element: null,
    
    options: {
        url       : '',
        duration  : 5000,
        el        : null,
        type      : 'image',
        validFrom : '',
        validTo   : '',
        html      : ''
    },
    initialize: function(options){
        this.setOptions(options);        
    },
    getURL: function(){
      return this.options.url;  
    },
    load: function(el){   
        
        console.log('Loading item:'+this.options.url);
        
        if(this.element != null){
            el.empty();
            this.element.inject(el);
            return;
        }
        
        this.options.el = el;
        
        //console.log('Loading item:'+this.options.url);
        
        if(this.options.type == 'image'){
            var image = new Element('img', {src : this.options.url, width : '100%', height : '100%'});
            el.empty();
            image.inject(el);  
            this.element = image;            
        }
        //console.log(this.options.type);
        if(this.options.type == 'post'){            
            var HTMLRequest = new Request.HTML({
                url: this.options.url,
                onSuccess : function(responseTree, responseElements, responseHTML, responseJavaScript){
                    var post = new Element('div', {html : responseHTML, 'class' : 'post'});                  
                    el.empty();
                    post.inject(el);
                    this.element = post;  
                    this.fireEvent('loaded', [this]);
                }.bind(this)
            }).send();  
            return;
        }
        if(this.options.type == 'video'){
            var video = new Element('video', {src : this.options.url, width : '100%', height : '100%'});
            el.empty();
            video.inject(el);  
            this.element = video;
        }
        if(this.options.type == 'text'){
            var text = new Element('div', {});
        }

        this.fireEvent('loaded', [this]); 
        
    },    
    show: function(){
        if(this.options.el.getParent('.preloading')){
            this.options.el.getParent('.preloading').removeClass('preloading');
        }
        console.log('Rendering item:'+this.options.url);
        var now       = new Date();
        var validFrom = Date.parse(this.options.validFrom);
        var validTo   = Date.parse(this.options.validTo);
        if(now < validFrom || now > validTo){
            this.fireEvent('end', [this, this.revision]);
            return false;
        }
        
        if(this.options.type == 'image'){            
            this.fireEvent('end', [this, this.revision], this.options.duration);           
        }
        //console.log(this.options.type);
        if(this.options.type == 'post'){   
            this.fireEvent('end', [this, this.revision], this.options.duration);           
        }
        if(this.options.type == 'video'){
            //FIX-ME: mootools does not support HTML5 events at the moment
            this.element.addEventListener('ended',function(evt){
                this.fireEvent('end', [this, this.revision]);
            }.bind(this),false); 
            this.element.play();
        }
        if(this.options.type == 'text'){
            var text = new Element('div', {});
        }                
        
        return true;
    },
    setRevision:function(revision){
        this.revision = revision;
    },
    getRevision:function(){
        return this.revision;
    }
    
});


